package com.adtech.farmProducts;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.view.KeyEvent;
import android.view.MenuItem;

import com.adtech.common.Common;
import com.adtech.farmProducts.activities.fragment.IndexFragment;
import com.adtech.farmProducts.activities.fragment.MineFragment;
import com.adtech.farmProducts.base.BaseActivity;
import com.adtech.farmProducts.base.BaseNfcActivity;
import com.adtech.farmProducts.base.Fragment;
import com.adtech.farmProducts.base.FragmentController;
import com.adtech.farmProducts.location.MyLocation;
import com.adtech.farmProducts.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Time:2020/6/19
 * Created by xiaocz
 * Describe
 */
public class IndexActivity extends BaseNfcActivity implements BottomNavigationView.OnNavigationItemSelectedListener{


    @BindView(R.id.bottom_navigation_view)
    BottomNavigationView bottomNavigationView;

    public static void show(Context mContext) {
        Intent intent=new Intent();
        intent.setClass(mContext,IndexActivity.class);
        mContext.startActivity(intent);
    }

    @Override
    protected int getLayoutSourceId() {
        return R.layout.index;
    }

    @Override
    protected void initWidgets() {
        super.initWidgets();
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        List<Fragment> mFragmentList = new ArrayList<>();
        mFragmentList.add(IndexFragment.newInstance());
        mFragmentList.add(MineFragment.newInstance());
//        // 初始化碎片控制的调度数据
        FragmentController.getInstance()
                .init(getSupportFragmentManager(), R.id.container_layout)
                .setFragments(mFragmentList);

        bottomNavigationView.setSelectedItemId(R.id.action_index);// 设置显示首页的碎片
        initLocation();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_index:
                FragmentController.getInstance().showFragment(0);
                break;
            case R.id.action_more:
                FragmentController.getInstance().showFragment(1);
                break;
        }
        return true;
    }

    private long mExitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 用户按下返回按键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - mExitTime > 2000L) {
                mExitTime = System.currentTimeMillis();
                App.getInstance().showMessage("再按一下将退出");
            } else {
                System.out.println("-------退出");
                AppUtil.finishAll();// 否则退出程序
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initLocation() {
        MyLocation myLocation = MyLocation.getInstance();
        myLocation.initLocation(getApplicationContext());
        myLocation.startLocation();
    }
}
